package com.xtool.livewallpaper

import android.app.Service
import android.content.Intent
import android.net.Uri
import android.os.Binder
import android.os.IBinder
import com.xtool.livewallpaper.transparent.TransparentWallpaperService
import com.xtool.livewallpaper.base.PrefsHelper
import com.xtool.livewallpaper.video.VideoWallpaperService

/**
 * @ClassName: WallPaperReceiveService
 * @Description:
 * @Author: Haiyang
 * @Date: 2021/7/27 21:09
 */
class WallPaperAliveService : Service() {
    companion object {
        const val TAG = "WallPaperAliveService"
        const val KEY_WALLPAPER = 0x001
        const val KEY_VIDEO_ALIVE = 0x002
        const val KEY_GIF_ALIVE = 0x003
        const val KEY_CAMERA_ALIVE = 0x004
        const val KEY_ALIVE_CODE = "alive_code"
        const val TYPE_ALIVE = 0
        const val TYPE_DEAD = 1

        const val KEY_WALLPAPER_TYPE = "type"
        const val TYPE_CAMERA = 0
        const val TYPE_GIF = 1
        const val TYPE_VIDEO = 2
        const val KEY_WALLPAPER_SRC = "src"

        var isVideoAlive = false
        var isCameraAlive = false
        var isGifAlive = false
    }

    private val binder: Binder = object : IWallPaperInterface.Stub() {
        override fun isVideoAlive(): Boolean {
            return isVideoAlive
        }

        override fun isCameraAlive(): Boolean {
            return isCameraAlive
        }

        override fun isGifAlive(): Boolean {
            return isGifAlive
        }

        override fun awake() {
            // Do nothing
        }

        override fun setVideoWallpaper(src: Uri?, isMute: Boolean) {
            src ?: return
            PrefsHelper.videoWallpaperPath = src.toString()
            PrefsHelper.isMute = isMute
            VideoWallpaperService.engineInstance?.updateWallPaper(isMute)
        }

        override fun setCameraWallpaper(isFront: Boolean) {
            PrefsHelper.isFrontCamera = isFront
            TransparentWallpaperService.instance?.setIsFront()
        }

        override fun setGifWallpaper(src: String?) {
            src ?: return
        }

        override fun setMute(isMute: Boolean) {
            PrefsHelper.isMute = isMute
            VideoWallpaperService.engineInstance?.setMute(isMute)
        }

        override fun pause() {
            VideoWallpaperService.engineInstance?.pause()
        }

        override fun play() {
            VideoWallpaperService.engineInstance?.play()
        }
    }

    override fun onBind(intent: Intent?): IBinder {
        return binder
    }
}